// 导入用于缓冲字符输出流的类
import java.io.BufferedWriter;
// 导入用于写入字符文件的类
import java.io.FileWriter;
// 导入用于处理输入输出异常的类
import java.io.IOException;
// 导入用于动态数组的类
import java.util.ArrayList;
// 导入用于映射键值对的接口
import java.util.Map;

/**
 * 数据存储类，用于将教师、学生、课程、教室和选课信息保存到文件中。
 */
public class DataStorage {

    /**
     * 将教师信息保存到文件中。
     *
     * @param teachers 教师信息的映射，键为教师ID，值为教师对象。
     * @param FilePath 文件保存的路径。
     */
    public static void saveTeachers(Map<String, Teacher> teachers, String FilePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FilePath + "/teachers.txt"))) {
            // 遍历教师信息映射
            for (Teacher teacher : teachers.values()) {
                // 将教师信息写入文件
                writer.write(teacher.getId() + "," + teacher.getName() + "," +
                        teacher.getGender() + "," + teacher.getBirthDate() + "," + teacher.getPhone()+","+teacher.getCouresnum());
                // 写入换行符
                writer.newLine();
            }
        } catch (IOException e) {
            // 打印异常堆栈信息
            e.printStackTrace();
        }
    }

    /**
     * 将学生信息保存到文件中。
     *
     * @param students 学生信息的映射，键为学生ID，值为学生对象。
     * @param FilePath 文件保存的路径。
     */
    public static void saveStudents(Map<String, Student> students, String FilePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FilePath + "/students.txt"))) {
            // 遍历学生信息映射
            for (Student student : students.values()) {
                // 将学生信息写入文件
                writer.write(student.getId() + "," + student.getName() + "," +
                        student.getGender() + "," + student.getBirthDate() + "," + student.getPhone()
                        +","+student.getClassid());
                // 写入换行符
                writer.newLine();
            }
        } catch (IOException e) {
            // 打印异常堆栈信息
            e.printStackTrace();
        }
    }

    /**
     * 将课程信息保存到文件中。
     *
     * @param courses  课程信息的映射，键为课程ID，值为课程对象。
     * @param FilePath 文件保存的路径。
     */
    public static void saveCourses(Map<String, Course> courses, String FilePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FilePath + "/courses.txt"))) {
            // 遍历课程信息映射
            for (Course course : courses.values()) {
                // 将课程信息写入文件
                writer.write(course.getCourseId() + "," + course.getName()
                        + "," + course.getHours() + "," + course.getCredits() + ","
                        + course.getStartWeek() + "," + course.getEndWeek() + ","
                        + course.getDayOfWeek() + "," + course.getClassPeriod() + "," + course.getLocation());
                // 写入换行符
                writer.newLine();
            }
        } catch (IOException e) {
            // 打印异常堆栈信息
            e.printStackTrace();
        }
    }

    /**
     * 将教室信息保存到文件中。
     *
     * @param classrooms 教室信息的映射，键为教室ID，值为教室对象。
     * @param FilePath   文件保存的路径。
     */
    public static void saveClassrooms(Map<String, Classroom> classrooms, String FilePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FilePath + "classrooms.txt"))) {
            // 遍历教室信息映射
            for (Classroom classroom : classrooms.values()) {
                // 将教室信息写入文件
                writer.write(classroom.getClassroomId() + "," + classroom.getName()
                        + "," + classroom.getCapacity());
                // 写入换行符
                writer.newLine();
            }
        } catch (IOException e) {
            // 打印异常堆栈信息
            e.printStackTrace();
        }
    }

    /**
     * 将选课信息保存到文件中。
     *
     * @param enrollments 选课信息的列表。
     * @param FilePath    文件保存的路径。
     */
    public static void saveEnrollments(ArrayList<Enrollment> enrollments, String FilePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FilePath + "enrollments.txt"))) {
            // 遍历选课信息列表
            for (Enrollment enrollment : enrollments) {
                // 将选课信息写入文件
                writer.write(enrollment.getCourseId() + "," + enrollment.getTeacherId() + ","
                        + enrollment.getStudentId() + "," + enrollment.getGrade()
                        + "," + enrollment.getFeedback()+","+enrollment.getClassroomId());
                writer.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void saveClasses(Map<String,Class> classes, String FilePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FilePath + "classes.txt"))) {
            // 遍历选课信息列表
            for (Class class1 : classes.values()) {
                // 将选课信息写入文件
                writer.write(class1.getClassid()+","+class1.getClassname());
                writer.newLine();
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void saveStucoursemsg(Map<String,stucoursemsg> stucoursemsgs, String FilePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FilePath + "stucoursemsgs.txt"))) {
            // 遍历选课信息列表
            for (stucoursemsg stucoursemsg : stucoursemsgs.values()) {
                // 将选课信息写入文件
                writer.write(stucoursemsg.getCourseId()+","+stucoursemsg.getClassID()+","+stucoursemsg.getTeacherID());
                writer.newLine();
            }
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}